var t = require("../../api.js"), a = getApp(), e = 0, i = 0, o = !0, r = 1, s = "", n = !1;
var h = require("../../api.js"), j = getApp(), ii = require("../../area-picker/area-picker.js");
function eee(ee) {
  a.onShowData || (a.onShowData = {}), a.onShowData.scene = ee;
}
Page({

    data: {
        sub_btn: true,
        xyshow:true,
        check:false,
        imageList: [],
        shoose:false,
        haibao:1,
        haibao_img:'',
        shop_is_tpl:'',
        form_id:''
    },
    onLoad: function (options) {
        a.pageOnLoad(this, options)
        var user_info = wx.getStorageSync('user_info');
        if (!user_info || !user_info.id) {
          this.setData({
            user_info_show: !0
          })
        }
        var that = this;
        that.getDistrictData(function (t) {
          ii.init({
            page: that,
            data: t
          });
        })
       a.request({
           url:h.default.tuzhang_type,
           method:'POST',
           success:function (e) {
                if(options.act==1){
                    that.setData({
                        code:0,
                        data:'',
                        info:'',
                        url:'',
                        shop_set:e.shop_set,
                        shop_is_tpl: e.shop_is_tpl
                    })
                }else{
                    that.setData({
                        code:e.code,
                        data:e.data,
                        info:e.info,
                        url:e.url,
                        shop_set:e.shop_set,
                        shop_is_tpl: e.shop_is_tpl
                    })
                }
           }
       })
    },
  getDistrictData: function (i) {
    var a = wx.getStorageSync("district");
    if (!a) return wx.showLoading({
      title: "正在加载",
      mask: !0
    }), void j.request({
      url: t.default.district,
      success: function (t) {
        wx.hideLoading(), 0 == t.code && (a = t.data, wx.setStorageSync("district", a),
          i(a));
      }
    });
    i(a);
  },
    tiaozhuan:function(){
      var that = this;
        wx.redirectTo({
            url:this.data.url,
            fail:function(){
              wx.switchTab({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                url: that.data.url
              })
            }
        })
    },

    onReady: function() {

    },

    onShow: function() {
      getApp().pageOnShow(this);
      var n = this;
      wx.setNavigationBarTitle({ 
        title:n.data.store.tz_name + '申请'
      })
    },

    onHide: function() {

    },
    ressadd:function(){ 
        var that = this;
        wx.chooseLocation({
            success: function(res) {
                that.setData({
                    longitude: res.longitude,
                    latitude: res.latitude,
                    address: res.address
                });
            },
            fail: function(a) {
                j.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
                    success: function(e) {
                        e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
                            title: "您取消了授权",
                            image: "/images/icon-warning.png"
                        }));
                    }
                });
            }
        });
    },
    hyxzboxshow: function (e) {
        this.setData({
            xyshow: false
        })
    },
    hyxzboxhide: function (e) {
        this.setData({
            xyshow: true,
            check:true,
            sub_btn: false
        })
    },
    image_show:function(){
      this.setData({
        haibao: 0
      })
    },
    read:function() {
        var check = this.data.check;
        if (check) {
            this.setData({
                check:false,
                sub_btn: true
            })
        } else {
            this.setData({
                check: true,
                sub_btn: false 
            })
        }
    },
    chooseImage: function () {
        var that = this
        wx.chooseImage({
            sourceType: ['album', 'camera'],
            count: 1,
            success: function (res) {
                that.setData({
                    imageList: res.tempFilePaths,
                    shoose:true
                })
            }
        })
    },
    formSubmitMessage: function (e) {
      var that = this;
      if (typeof wx.requestSubscribeMessage === "function") {
        var shop_is_tpl = that.data.shop_is_tpl;
        return wx.requestSubscribeMessage({
          tmplIds: [shop_is_tpl],
          success: function (res) {
            if (shop_is_tpl != '' && res[shop_is_tpl] == "accept") {
              that.data.form_id = shop_is_tpl
            }
          },
          fail: function (res) {

          },
          complete: function (res) {
            that.read(e);
          }
        })
      } else {
        that.read(e);
      }
    },
    formSubmit:function (e) {
      a.request({
        url: t.default.login_style,
        method: "POST",
        success: function (t) {
          // console.log(t);
          if (t == 0) {
            wx.redirectTo({
              url: "/pages/login/login"
            })
          }
        },
      })

        var that = this;
        let {address,latitude,locationAddress,longitude,mobile,wx_code,name} = e.detail.value;
        if (!that.data.imageList[0]) {
          wx.showModal({
            title: '提示',
            content: '请上传头像',
            showCancel: false,
          })
          return false;
        }
      if (!that.data.district){ 
        wx.showModal({
          title: '提示',
          content: '请选择地区',
          showCancel: false,
        })
        return false;
      }
        wx.showLoading({
            title: '提交中',
        });
      
        var upload_task = wx.uploadFile({
            url: h.default.upload_img,
            filePath:that.data.imageList[0] ,
            name: 'file',
            formData: {
                address:address+locationAddress,
                latitude:latitude,
                longitude:longitude,
                mobile:mobile,
                name:name,
            },
            success: function (res) {
                var data_obj = JSON.parse(res.data);
                if(data_obj.errCode>0){
                    wx.showModal({
                        title:'提示',
                        content: data_obj.errMsg,
                        showCancel:false,
                    })
                }
                if(data_obj.errCode==0){
                    var pic_img = data_obj.file;
                    a.request({
                        url: h.default.tuzhang_save,
                        method:'POST',
                        data:{
                            address:address+locationAddress,
                            latitude:latitude,
                            longitude:longitude,
                            mobile:mobile,
                            wx_code: wx_code,
                            name:name,
                            pic_url:pic_img,
                            cover_url:pic_img,
                            formId:that.data.form_id,
                            province: that.data.district.province.id,
                            city: that.data.district.city.id,
                            district: that.data.district.district.id,
                            tz_apply_price: that.data.shop_set.tz_apply_price,
                        },
                        success:function (t) {
                          if(that.data.shop_set && that.data.shop_set.tz_apply_price > 0) {
                            if (0 == t.code) return setTimeout(function() {
                                wx.hideLoading();
                            }, 1e3), eee("pay"), void wx.requestPayment({
                                _res: t,
                                timeStamp: t.data.timeStamp,
                                nonceStr: t.data.nonceStr,
                                package: t.data.package,
                                signType: t.data.signType,
                                paySign: t.data.paySign,
                                success: function(e) {},
                                fail: function(e) {},
                                complete: function(e) {
                                    "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? wx.showModal({
                                        title: "提示",
                                        content: "申请成功",
                                        showCancel: !1,
                                        confirmText: "确认",
                                        success: function(e) {
                                            e.confirm && wx.navigateBack({
                                                delta: 1
                                            });
                                        }
                                    }) : wx.showModal({
                                        title: "提示",
                                        content: "尚未支付",
                                        showCancel: !1,
                                        confirmText: "确认"
                                    });
                                }
                            });
                            wx.showModal({
                                title: "提示",
                                content: t.info,
                                showCancel: !1
                            }), wx.hideLoading();
                        } else {
                            wx.hideLoading(), 0 == t.code && wx.showModal({
                              title:'提示',
                              content: t.info,
                              showCancel:false,
                              success:function (e) {
                                wx.switchTab({    //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                                  url: "/pages/index/index"
                                })
                              }
                            }), 1 == t.code && wx.showModal({
                              title:'提示',
                              content: t.info,
                              showCancel:false,
                          });
            
                        }
                            

                        },
                        fail:function () {
                            wx.showModal({
                                title:'提示',
                                content: '提交失败',
                                showCancel:false,
                            })
                        }
                    })
                }

            },
            complete: function () {
              wx.hideLoading();
            }
        })



    },

    onAreaPickerConfirm: function (t) {
      this.setData({
        district: {
          province: {
            id: t[0].id,
            name: t[0].name
          },
          city: {
            id: t[1].id,
            name: t[1].name
          },
          district: {
            id: t[2].id,
            name: t[2].name
          }
        }
      });
    },


})